Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Filter with startsWith with multiple object keys

I am making a search component and everything works well. But I have a question. How do I make the following code dynamic?

function isMatch(obj) {
  let result =  obj.code.toLowerCase().startsWith(search.toLowerCase()) || 
  obj.description.toLowerCase().startsWith(search.toLowerCase()) 
  return  result 
}

Now the search is carried out on two keys of the object, but I need to set it dynamically. For example:

serachKey = ['code', 'description', ...]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's one approach you can try:

const searchKey = ['code', 'description'];

function isMatch(obj, search) {
  return searchKey.some(key => obj[key] && obj[key].toLowerCase().startsWith(search.toLowerCase()));
}

const mySearch = "testing";
const myObj = {"code": "some code", "description": "testing code"};

console.log(isMatch(myObj, mySearch));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!